#include "CApp.h"

bool CApp::OnInit()
{
	BuildDebugConsole();
	ConsolePrint("Initialized Debug Console\n");

	char LocalDir[MAX_PATH];	
	if( GetModuleFileNameA( NULL, LocalDir, MAX_PATH ) != 0 )
	{
		for( int i = ( int )strlen( LocalDir ); i > 0; i-- )
		{
			if( LocalDir[ i ] == '\\' )
			{				
				LocalDir[ i + 1 ] = 0;
				break;
			}	
		}
	}
	m_Path = std::string(LocalDir);

	ConsolePrint("Game running at directory %s\n", LocalDir);

	ConsolePrint("Registering and loading cvars\n");
	VarManager vmgr(m_Path);

	int _width = reinterpret_cast<IntVar*>(vmgr.Get("screen.width"))->getValue();
	int _height = reinterpret_cast<IntVar*>(vmgr.Get("screen.height"))->getValue();
	int _bpp = reinterpret_cast<IntVar*>(vmgr.Get("screen.bpp"))->getValue();
	bool _fullscreen = reinterpret_cast<BoolVar*>(vmgr.Get("screen.fullscreen"))->getValue();

	sf::ContextSettings Settings;
	Settings.DepthBits         = 24; // Request a 24 bits depth buffer
	Settings.StencilBits       = 8;  // Request a 8 bits stencil buffer
	Settings.AntialiasingLevel = 2;  // Request 2 levels of antialiasing

	if(_fullscreen == false)
		m_Window.Create(sf::VideoMode(_width, _height, _bpp), m_windowTitle, sf::Style::Close, Settings);
	else
		m_Window.Create(sf::VideoMode(_width, _height, _bpp), m_windowTitle, sf::Style::Fullscreen, Settings);

	bool _vsync = reinterpret_cast<BoolVar*>(vmgr.Get("graphics.vsync"))->getValue();
	m_Window.EnableVerticalSync(_vsync);

	int _fpslimit = reinterpret_cast<IntVar*>(vmgr.Get("graphics.fpslimit"))->getValue();
	m_Window.SetFramerateLimit(_fpslimit);

	return true;
}